<template>
    <div class="mui-numbox" data-numbox-min='1' :data-numbox-max='itemcoun'>
		<button class="mui-btn mui-btn-numbox-minus" type="button" @click='change(-1)'>-</button>
		<input id="test" class="mui-input-numbox" type="number"  @change="countChanged" @keydown.enter="change(value,1)" ref="num" v-model="value" />
		<button class="mui-btn mui-btn-numbox-plus" type="button" @click="change(1)">+</button>
	</div>
</template>

<script>
import mui from '../../lib/mui/dist/js/mui.js'
import { Toast } from 'mint-ui'
export default {
    data(){
        return{
            // value: 1,
            sub:0
        }
    },
    props:['itemcoun','value','id'],
    mounted(){
        mui('.mui-numbox').numbox()
    },
    methods:{
        countChanged(){
            if (this.itemcoun == undefined) {
                var obj = {
                    id: this.id,
                    count: this.value,
                    reset: true
                }
                this.$store.commit('addcar',obj)
            }else{
                var shuliang = this.$refs.num.value
                if ( shuliang <=this.itemcoun && shuliang > 0 ){
                    this.$emit('change',parseInt(shuliang))
                }else {
                    Toast('请输入合法数字')
                    this.$refs.num.value = 1
                }    
            }
        },
        change(num,a){
            if (this.itemcoun == undefined) {
                var obj = {
                    id: this.id,
                    count: num,
                    reset: true
                }
                if (num == 1 || num == -1 && a != 1) obj.flag = true
            this.$store.commit('addcar',obj)
            }
        }
    },
    watch:{

    }
}
</script>

<style scoped>

</style>